home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap17 / EZTest / EZFont.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  2.5 KB  |  80 lines

  1. /*---------------------------------------
  2.    EZFONT.C -- Easy Font Creation
  3.                (c) Charles Petzold, 1998
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <math.h>
  8. #include "ezfont.h"
  9.  
  10. HFONT EzCreateFont (HDC hdc, TCHAR * szFaceName, int iDeciPtHeight,
  11.                     int iDeciPtWidth, int iAttributes, BOOL fLogRes)
  12. {
  13.      FLOAT      cxDpi, cyDpi ;
  14.      HFONT      hFont ;
  15.      LOGFONT    lf ;
  16.      POINT      pt ;
  17.      TEXTMETRIC tm ;
  18.      
  19.      SaveDC (hdc) ;
  20.      
  21.      SetGraphicsMode (hdc, GM_ADVANCED) ;
  22.      ModifyWorldTransform (hdc, NULL, MWT_IDENTITY) ;
  23.      SetViewportOrgEx (hdc, 0, 0, NULL) ;
  24.      SetWindowOrgEx   (hdc, 0, 0, NULL) ;
  25.      
  26.      if (fLogRes)
  27.      {
  28.           cxDpi = (FLOAT) GetDeviceCaps (hdc, LOGPIXELSX) ;
  29.           cyDpi = (FLOAT) GetDeviceCaps (hdc, LOGPIXELSY) ;
  30.      }
  31.      else
  32.      {
  33.           cxDpi = (FLOAT) (25.4 * GetDeviceCaps (hdc, HORZRES) /
  34.                                         GetDeviceCaps (hdc, HORZSIZE)) ;
  35.           
  36.           cyDpi = (FLOAT) (25.4 * GetDeviceCaps (hdc, VERTRES) /
  37.                                         GetDeviceCaps (hdc, VERTSIZE)) ;
  38.      }
  39.      
  40.      pt.x = (int) (iDeciPtWidth  * cxDpi / 72) ;
  41.      pt.y = (int) (iDeciPtHeight * cyDpi / 72) ;
  42.      
  43.      DPtoLP (hdc, &pt, 1) ;
  44.      
  45.      lf.lfHeight         = - (int) (fabs (pt.y) / 10.0 + 0.5) ;
  46.      lf.lfWidth          = 0 ;
  47.      lf.lfEscapement     = 0 ;
  48.      lf.lfOrientation    = 0 ;
  49.      lf.lfWeight         = iAttributes & EZ_ATTR_BOLD      ? 700 : 0 ;
  50.      lf.lfItalic         = iAttributes & EZ_ATTR_ITALIC    ?   1 : 0 ;
  51.      lf.lfUnderline      = iAttributes & EZ_ATTR_UNDERLINE ?   1 : 0 ;
  52.      lf.lfStrikeOut      = iAttributes & EZ_ATTR_STRIKEOUT ?   1 : 0 ;
  53.      lf.lfCharSet        = DEFAULT_CHARSET ;
  54.      lf.lfOutPrecision   = 0 ;
  55.      lf.lfClipPrecision  = 0 ;
  56.      lf.lfQuality        = 0 ;
  57.      lf.lfPitchAndFamily = 0 ;
  58.      
  59.      lstrcpy (lf.lfFaceName, szFaceName) ;
  60.      
  61.      hFont = CreateFontIndirect (&lf) ;
  62.      
  63.      if (iDeciPtWidth != 0)
  64.      {
  65.           hFont = (HFONT) SelectObject (hdc, hFont) ;
  66.           
  67.           GetTextMetrics (hdc, &tm) ;
  68.           
  69.           DeleteObject (SelectObject (hdc, hFont)) ;
  70.           
  71.           lf.lfWidth = (int) (tm.tmAveCharWidth *
  72.                                         fabs (pt.x) / fabs (pt.y) + 0.5) ;
  73.           
  74.           hFont = CreateFontIndirect (&lf) ;
  75.      }
  76.      
  77.      RestoreDC (hdc, -1) ;
  78.      return hFont ;
  79. }
  80.